<template>
    <view style="position: fixed; bottom: 30rpx; right: 30rpx;">
<!--        <view v-if="autoRefresh" @tap="_autoRefresh" class="round text-white flex align-center justify-center" style="width: 80rpx; height: 80rpx; background-color: rgba(0,0,0,.3)">
            <view v-if="auto_refresh.run == false" class="text-sm text-bold">
                <view>自动</view>
                <view>刷新</view>
            </view>
            <view v-else class="text-xl">
                {{auto_refresh.current_items}}
            </view>
        </view> -->
        <view v-if="tapRefresh" @tap="_tapRefresh" class="margin-top-20 round text-white flex align-center justify-center"
            style="width: 80rpx; height: 80rpx; background-color: rgba(0,0,0,.3)">
            <text class="text-bold" style="font-size: 50rpx;"><text class="cuIcon-refresh"></text></text>
        </view>
    </view>
</template>

<script>
    export default {
        props: ['autoRefresh', 'tapRefresh'],
        data() {
            return {
                auto_refresh: {
                    run: false,
                    max_times: 30,
                    current_items: 30,
                    interval: null,
                }
            }
        },
        methods: {
            _autoRefresh() {
                if (this.auto_refresh.run == true) {
                    this.auto_refresh.run = false;
                    this.auto_refresh.current_items = this.auto_refresh.max_times;
                    clearInterval(this.auto_refresh.interval);
                    this.auto_refresh.interval = null;
                    return;
                }
                if (!this.auto_refresh.interval) {
                    this.auto_refresh.run = true;
                    let interval = () => {
                        this.auto_refresh.current_items -= 1;
                        if (this.auto_refresh.current_items <= 0) {
                            this.auto_refresh.current_items = this.auto_refresh.max_times - 1;
                            this.autoRefresh();
                        }
                    };
                    interval();
                    this.auto_refresh.interval = setInterval(interval, 1000)
                }
            },
            _tapRefresh() {
                this.tapRefresh();
            },
        }
    }
</script>
